package com.sc.nft.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.*;
import com.sc.nft.controller.vo.CatenaVO;
import com.sc.nft.dao.OrderDao;
import com.sc.nft.dao.SecondOrderDao;
import com.sc.nft.dao.UserCityServerCentreDao;
import com.sc.nft.dao.UserWorkRoomDao;
import com.sc.nft.entity.*;
import com.sc.nft.entity.dto.ConfirmExchangeSubscribeDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.NftMallService;
import com.sc.nft.service.UserPerformanceService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 服务控制器
 *
 * @author Sun
 * @since 2022-05-30 13:42:06
 * @description 由 Mybatisplus Code Generator 创建
 *
 * @Slf4j
 * @RequiredArgsConstructor
 * @RestController
 * @RequestMapping("/user")
 * @Api(tags ="用户")
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/mall")
@Api(tags ="商城页面")
public class NftMallController {

    private final NftMallService nftMallService;

    @ApiOperation(value = "获取系列列表", notes = "获取系列列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getCatenaList", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<CatenaVO>> getCatenaList(@Valid @RequestBody GetCatenaRequest request) {
        List<CatenaInfo> list =  nftMallService.getCatenaListByType(request.getType());
        return Result.successColl(list,CatenaVO.class);
    }

    private final UserWorkRoomDao userWorkRoomDao;
    private final UserCityServerCentreDao userCityServerCentreDao;
    private final OrderDao orderDao;
    private final UserPerformanceService userPerformanceService;
    private final SecondOrderDao secondOrderDao;
  @ApiOperation(value = "test", notes = "test", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result<List<CatenaVO>> test( ) {
      Map<Long,Integer> map = Maps.newHashMap();
      for (UserWorkRoom userWorkRoom : userWorkRoomDao.list()) {
          map.put(userWorkRoom.getUserId(),1);
      }
      for (UserCityServerCentre userCityServerCentre : userCityServerCentreDao.list()) {
          map.put(userCityServerCentre.getUserId(),2);
      }
      for (Order order : orderDao.getWithSettlePerformance()) {
          String date = DateUtil.format(order.getOrderTime(),DATE_FORMAT);
          userPerformanceService.calFirstOrder(order,date,map);
      }
      for (SecondOrder order : secondOrderDao.getWithSettlePerformance()) {
          String date = DateUtil.format(order.getDealTime(),DATE_FORMAT);
          userPerformanceService.calSecondOrder(order,date,map);
      }
        return Result.success();
    }

    public static final String DATE_FORMAT="yyyyMMdd";

    @ApiOperation(value = "获取商城列表", notes = "获取商城列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getMallPageList", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<DigitalCollectionProductVO> getMallPageList(@Valid @RequestBody MallPageListRequest mallPageListRequest) {
        Page<DigitalCollectionProductVO> page = nftMallService.getMallPageList(mallPageListRequest.getCatenaId(),
                                                                           mallPageListRequest.getCount(),
                                                                           mallPageListRequest.getNum(),
                                                                           mallPageListRequest.getTime(),
                                                                           mallPageListRequest.getTypeClassify(),
                                                                           mallPageListRequest.getPageNo(),
                                                                           mallPageListRequest.getPageSize(),
                                                                           mallPageListRequest.getAppStoreVersion());

        return PageResult.success(page);
    }

    @ApiOperation(value = "获取数字藏品详情", notes = "获取数字藏品详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getProductDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin( required = false)
    public Result<ProductDetailsVO> getProductDetails(@Valid @RequestBody ProductDetailsRequest productDetailsRequest) {
        return Result.success(nftMallService.getProductDetails(productDetailsRequest.getProductId(),productDetailsRequest.getAppStoreVersion()));
    }

    @ApiOperation(value = "获取申购数字藏品详情", notes = "获取申购数字藏品详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getSubscribeProductDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<SubscribeProductDetailsVO> getSubscribeProductDetails(@Valid @RequestBody ProductDetailsRequest productDetailsRequest) {
        return Result.success(nftMallService.getSubscribeProductDetails(productDetailsRequest.getProductId(),productDetailsRequest.getAppStoreVersion()));
    }

    @ApiOperation(value = "获取拍卖数字藏品详情", notes = "获取拍卖数字藏品详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getAuctionProductDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<AuctionProductDetailsVO> getAuctionProductDetails(@Valid @RequestBody ProductDetailsRequest productDetailsRequest) {
        return Result.success(nftMallService.getAuctionProductDetails(productDetailsRequest.getProductId(),productDetailsRequest.getAppStoreVersion()));
    }

    @ApiOperation(value = "获取盲盒详情", notes = "获取盲盒详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getBlindBoxDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin( required = false)
    public Result<BlindBoxDetailsVO> getBlindBoxDetails(@Valid @RequestBody ProductDetailsRequest productDetailsRequest)  {
        return Result.success(nftMallService.getBlindBoxDetails(productDetailsRequest.getProductId(),productDetailsRequest.getAppStoreVersion()));
    }

    @ApiOperation(value = "搜索盲盒/数字藏品", notes = "搜索盲盒/数字藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/searchProductList", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin( required = false)
    public Result<Page<DigitalCollectionProductVO>> searchProductList(@Valid @RequestBody SearchProductListRequest searchProductListRequest) {
        Page<DigitalCollectionProductVO> page =nftMallService.searchProductList(searchProductListRequest.getFirstTitle(),searchProductListRequest.getTypeClassify(),searchProductListRequest.getPageNo(),searchProductListRequest.getPageSize(),searchProductListRequest.getAppStoreVersion());
        return PageResult.success(page);
    }

    @ApiOperation(value = "确认申购兑换", notes = "确认申购归还", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/confirmExchangeSubscribe", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result confirmExchangeSubscribe(@Valid @RequestBody ConfirmExchangeSubscribeDTO confirmExchangeSubscribeDTO) {
        nftMallService.confirmExchangeSubscribe(confirmExchangeSubscribeDTO);
        return PageResult.success();
    }

    @ApiOperation(value = "中签申购码", notes = "中签申购码", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getSubscribeWin", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<MySubscribeCodesWinDataVo> getSubscribeWin(@Valid @RequestBody MySubscribeCodesRequest mySubscribeCodesRequest) {
        return PageResult.success(nftMallService.getSubscribeWin(mySubscribeCodesRequest.toMp(),
                mySubscribeCodesRequest.getProductId()));
    }


}